Chapter 2, Section 2.1, part 3. Boolean algebra, bitwise operations, shifts 2.17, 2.18 Boolean Algebra and C bitwise operators. 1. AND, OR, EXOR, and NOT NOT AND OR EXOR ~ | & | 0 1 | | 0 1 ^ | 0 1 --+--- --+------ --+------ --+------ 0 | 1 0 | 0 0 0 | 0 1 0 | 0 1 | | | | 1 | 0 1 | 0 1 1 | 1 1 1 | 1 0 C Binary Binary expression expression result C result ~0x41 ~[01000001] [10111110] 0xBE ~0x00 ~[00000000] [11111111] 0xFF 0x69 & 0x55 ~[01101001] & [01010101] [01000001] 0x41 0x69 | 0x55 ~[01101001] | [01010101] [01111101] 0x7D See practice problem 2.8, 2.10, 2.11, 2.12 2. How many of these can you name? +-------+ A (0 or 1) ---->| | | |----> output (0 or 1) B (0 or 1) ---->| | +-------+ --------- ---------- ---------- ---------- B B B B | 0 1 | 0 1 | 0 1 A | 0 1 --+------ --+------- --+------- --+------- 0 | 0 0 0 | 0 0 0 | 0 0 0 | 0 0 A | A | A | A | 1 | 0 0 1 | 0 1 1 | 1 0 1 | 1 1 --------- ---------- ---------- ---------- B B B B | 0 1 | 0 1 | 0 1 | 0 1 --+------ --+------- --+------- --+------- 0 | 0 1 0 | 0 1 0 | 0 1 0 | 0 1 A | A | A | A | 1 | 0 0 1 | 0 1 1 | 1 0 1 | 1 1 --------- ---------- ---------- ---------- B B B B | 0 1 | 0 1 | 0 1 | 0 1 --+------ --+------- --+------- --+------- 0 | 1 0 0 | 1 0 0 | 1 0 0 | 1 0 A | A | A | A | 1 | 0 0 1 | 0 1 1 | 1 0 1 | 1 1 --------- ---------- ---------- ---------- B B B B | 0 1 | 0 1 | 0 1 | 0 1 --+------ --+------- --+------- --+------- 0 | 1 1 0 | 1 1 0 | 1 1 0 | 1 1 A | A | A | A | 1 | 0 0 1 | 0 1 1 | 1 0 1 | 1 1 3. All 16 Boolean functions and C bitwise expressions. ZERO AND 0 A & B A & ~B A B B B B 0 | 0 1 & | 0 1 | 0 1 A | 0 1 --+------ --+------- --+------- --+------- 0 | 0 0 0 | 0 0 0 | 0 0 0 | 0 0 A | A | A | A | 1 | 0 0 1 | 0 1 1 | 1 0 1 | 1 1 EXOR OR ~A & B B A ^ B A | B B B B B | 0 1 B | 0 1 ^ | 0 1 | | 0 1 --+------ --+------- --+------- --+------- 0 | 0 1 0 | 0 1 0 | 0 1 0 | 0 1 A | A | A | A | 1 | 0 0 1 | 0 1 1 | 1 0 1 | 1 1 NOR equal B->A ~(A|B) ~(A^B) ~B ~B|A B B B B | 0 1 | 0 1 | 0 1 | 0 1 --+------ --+------- --+------- --+------- 0 | 1 0 0 | 1 0 0 | 1 0 0 | 1 0 A | A | A | A | 1 | 0 0 1 | 0 1 1 | 1 0 1 | 1 1 NOT A A->B NAND ONE ~A ~A|B ~(A&B) 0xffff B B B B | 0 1 | 0 1 | 0 1 | 0 1 --+------ --+------- --+------- --+------- 0 | 1 1 0 | 1 1 0 | 1 1 0 | 1 1 A | A | A | A | 1 | 0 0 1 | 0 1 1 | 1 0 1 | 1 1 2.1.9 Logical operations in c. 1. || = OR, && = AND, ! = NOT 2. 0 represents False, any non-zero value represents True 3. Don't evaluate second argument if it won't effect result, so (a && 5/a) will never cause divison by zero. 4. Expression Result !0x41 0x00 !0x00 0x01 !!0x41 0x01 0x69 && 0x55 0x01 0x69 || 0x55 0x01 5. Practice problems 2.13 2.1.10. Shift operatins in C 1. x << k shifts x left k bits, filling the rightmost k bits with zeros. 2. x >> k shifts x right k bits, filling the leftmost bits with: a. unsigned numbers, fill with zeros. b. signed numbers, fill with a copy of the original leftmost bit (sign bit). 3. Practice Problem 2.15